package Controler.Stage;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Controler.Fonctions;
import JDBC.Stage_Afficher;
import JDBC.Stage_Ajouter;

import JDBC.Stage_Ajouter;
import Model.Stage;
import Model.Listes;
import Model.Stage;

/**
 * Servlet implementation class Stage_CreationStageController
 */
@WebServlet("/Stage_CreationStageController")
public class Stage_CreationStageController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Stage_CreationStageController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		{
			String url = "Vues/VuesGrp3/vueGRP3_AfficheStage.jsp";
			boolean heure=false;
			boolean date=false;
			
			if(request.getParameter("ajoutMatiere")!=null)
			{
			url="Vues/Matiere/vueGRP1_ajouter.jsp";
			}
			
			if(request.getParameter("ajoutLieu")!=null)
			{
			url="Vues/Lieu/vueGRP3_ajouter.jsp";
			}
	
			
			if(request.getParameter("ok")!=null)
			{
			
				String duree = request.getParameter("duree");
				String d1 = request.getParameter("d1");
				String d2 = request.getParameter("d2");
				String d3 = request.getParameter("d3");
				String hd1 = request.getParameter("hd1");
				String hd2 = request.getParameter("hd2");
				String hf1 = request.getParameter("hf1");
				String hf2 = request.getParameter("hf2");
				
				if(duree.equals("") || d1.equals("") || d2.equals("") || d3.equals("") || hd1.equals("") || hd2.equals("")
						|| hf1.equals("") || hf2.equals(""))
				{
					url = "Vues/VuesGrp3/VuesGrp3_Erreur/erreurCreationStage.jsp";
				}
				else
				{
					String dateStage = d1+"-"+d2+"-"+d3;
					String heureDebut = hd1+":"+hd2;
					String heureFin = hf1+":"+hf2;
					int heuredebut=0;
					int minutedebut=0;
					int heurefin=0;
					int minuteFin=0;
					int dureeStage=0;
					
					try
					{
						heuredebut = Integer.parseInt(hd1);
						minutedebut = Integer.parseInt(hd2);
						heurefin = Integer.parseInt(hf1);
						minuteFin = Integer.parseInt(hf2);
						dureeStage = Integer.parseInt(duree);
						
					}
					catch (Exception e)
					{
						url = "Vues/VuesGrp3/VuesGrp3_Erreur/erreurCreationStage.jsp";
					}
					

					
					if(Fonctions.verifierHeure(heureDebut) && Fonctions.verifierHeure(heureFin))
					{
						if(Fonctions.comparerHeure(heureDebut,heureFin))
						{
							heure=true;
						}
					}
					

					if(Fonctions.verifierDate(dateStage))
					{
						date=true;
					}
					

					
					if(heure && date)
					{
						Stage s = new Stage();
						String[] idEleves=null;
						
					
						Stage_Ajouter stage= new Stage_Ajouter();
						idEleves=(request.getParameterValues("eleves"));
						s.setDateStage(dateStage);
						s.setDuree(dureeStage);
						s.setHeureDebut(heureDebut);
						s.setHeureFin(heureFin);
						s.setId_Mat(Integer.parseInt(request.getParameter("matiere")));
						s.setId_Niv(Integer.parseInt(request.getParameter("niveau")));
						s.setId_Prof(Integer.parseInt(request.getParameter("professeur")));
						s.setCommentaire(request.getParameter("commentaire"));
						s.setId_Lieu(Integer.parseInt(request.getParameter("lieu"))); //  à modifier lorsqu'on aura module crud lieu
							 // on ne récupère pas l'id avec la jsp mais avec la méthode Stage_SélectionLieu
					
						int idStage=stage.ajouter(s);
						for (int i = 0; i < idEleves.length; i++) {
							stage.associerElevesStage(idEleves[i], idStage);
						}
						
					}
					else
					{
						url = "Vues/VuesGrp3/VuesGrp3_Erreur/erreurCreationStage.jsp";
					}
				}
			}
			
			if(request.getParameter("out")!=null)
			{
				url = "Vues/VuesGrp3/vueGRP3_AfficheStage.jsp";
			}
			
			HttpSession session = request.getSession();
			Listes listes = (Listes) session.getAttribute("listes");
			
			if (listes==null)
			{
				listes = new Listes();
			}
			
			Stage_Afficher listeStage = new Stage_Afficher();
			ArrayList<Stage> mesStage = listeStage.afficherStage();
			listes.setMesStages(mesStage);
			session.setAttribute("listes", listes);
			request.getRequestDispatcher(url).forward(request, response);
		}
	}

}
